﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:toms="urn:TomsToolbox"
                    xmlns:styles="clr-namespace:tomenglertde.ResXManager.Styles">

  <MenuScrollingVisibilityConverter x:Key="MenuScrollingVisibilityConverter" />

  <Geometry x:Key="DownArrow">M 0,0 L 3.5,4 L 7,0 Z</Geometry>
  <Geometry x:Key="UpArrow">M 0,4 L 3.5,0 L 7,4 Z</Geometry>
  <Geometry x:Key="RightArrow">M 0,0 L 4,3.5 L 0,7 Z</Geometry>

  <Style x:Key="MenuScrollButton" BasedOn="{x:Null}" TargetType="{x:Type RepeatButton}">
    <Setter Property="ClickMode" Value="Hover" />
    <Setter Property="MinWidth" Value="0" />
    <Setter Property="MinHeight" Value="0" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type RepeatButton}">
          <Grid Background="Transparent" SnapsToDevicePixels="True">
            <Border BorderThickness="1" BorderBrush="Transparent">
              <ContentPresenter x:Name="ContentContainer" Margin="0,2,0,2" HorizontalAlignment="Center" VerticalAlignment="Center" />
            </Border>
          </Grid>
          <ControlTemplate.Triggers>
            <Trigger Property="IsPressed" Value="True">
              <Setter TargetName="ContentContainer" Property="Margin" Value="3,3,1,1" />
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <Style x:Key="MenuScrollViewer" BasedOn="{x:Null}" TargetType="{x:Type ScrollViewer}">
    <Setter Property="HorizontalScrollBarVisibility" Value="Hidden" />
    <Setter Property="VerticalScrollBarVisibility" Value="Auto" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type ScrollViewer}">
          <Grid SnapsToDevicePixels="True">
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
              <RowDefinition Height="Auto" />
              <RowDefinition Height="*" />
              <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Border Grid.Row="1" Grid.Column="0">
              <ScrollContentPresenter Margin="{TemplateBinding Padding}" />
            </Border>
            <RepeatButton Grid.Row="0" Grid.Column="0" Command="{x:Static ScrollBar.LineUpCommand}"
                          CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Focusable="False"
                          Style="{StaticResource MenuScrollButton}">
              <RepeatButton.Visibility>
                <MultiBinding Converter="{StaticResource MenuScrollingVisibilityConverter}" ConverterParameter="0"
                              FallbackValue="Visibility.Collapsed">
                  <Binding Path="ComputedVerticalScrollBarVisibility" RelativeSource="{RelativeSource TemplatedParent}" />
                  <Binding Path="VerticalOffset" RelativeSource="{RelativeSource TemplatedParent}" />
                  <Binding Path="ExtentHeight" RelativeSource="{RelativeSource TemplatedParent}" />
                  <Binding Path="ViewportHeight" RelativeSource="{RelativeSource TemplatedParent}" />
                </MultiBinding>
              </RepeatButton.Visibility>
              <Path Data="{StaticResource UpArrow}" Fill="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}" />
            </RepeatButton>
            <RepeatButton Grid.Row="2" Grid.Column="0" Command="{x:Static ScrollBar.LineDownCommand}"
                          CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Focusable="False"
                          Style="{StaticResource MenuScrollButton}">
              <RepeatButton.Visibility>
                <MultiBinding Converter="{StaticResource MenuScrollingVisibilityConverter}" ConverterParameter="100"
                              FallbackValue="Visibility.Collapsed">
                  <Binding Path="ComputedVerticalScrollBarVisibility" RelativeSource="{RelativeSource TemplatedParent}" />
                  <Binding Path="VerticalOffset" RelativeSource="{RelativeSource TemplatedParent}" />
                  <Binding Path="ExtentHeight" RelativeSource="{RelativeSource TemplatedParent}" />
                  <Binding Path="ViewportHeight" RelativeSource="{RelativeSource TemplatedParent}" />
                </MultiBinding>
              </RepeatButton.Visibility>
              <Path Data="{StaticResource DownArrow}" Fill="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}" />
            </RepeatButton>
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <Style x:Key="{x:Static styles:ResourceKeys.MenuItemSeparatorStyle}" TargetType="{x:Type Separator}">
    <Setter Property="Height" Value="1" />
    <Setter Property="Margin" Value="25,2,0,2" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type Separator}">
          <Border BorderBrush="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"
                  BorderThickness="1" />
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <ControlTemplate x:Key="MenuItemTopLevelHeaderTemplate" TargetType="{x:Type MenuItem}">
    <Grid>
      <ContentPresenter Margin="{TemplateBinding Padding}" ContentSource="Header" RecognizesAccessKey="True" />
      <Popup x:Name="Popup" AllowsTransparency="True" Focusable="False" IsOpen="{TemplateBinding IsSubmenuOpen}"
             Placement="Bottom" PopupAnimation="Fade">
        <Border x:Name="SubmenuBorder"
                Background="{DynamicResource {x:Static SystemColors.MenuBarBrushKey}}"
                BorderBrush="{DynamicResource {x:Static styles:ResourceKeys.BorderBrush}}"
                BorderThickness="1" SnapsToDevicePixels="True">
          <ScrollViewer CanContentScroll="True" Style="{StaticResource MenuScrollViewer}">
            <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="True" />
          </ScrollViewer>
        </Border>
      </Popup>
    </Grid>
    <ControlTemplate.Triggers>
      <Trigger Property="IsSuspendingPopupAnimation" Value="True">
        <Setter TargetName="Popup" Property="PopupAnimation" Value="None" />
      </Trigger>
      <Trigger Property="IsHighlighted" Value="True">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}" />
      </Trigger>
      <Trigger SourceName="Popup" Property="AllowsTransparency" Value="True">
        <Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="0,0,4,4" />
      </Trigger>
      <Trigger Property="IsEnabled" Value="False">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
      </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>

  <ControlTemplate x:Key="MenuItemTopLevelItemTemplate" TargetType="{x:Type MenuItem}">
    <ContentPresenter Margin="{TemplateBinding Padding}" ContentSource="Header" RecognizesAccessKey="True" />
    <ControlTemplate.Triggers>
      <Trigger Property="IsHighlighted" Value="True">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}" />
      </Trigger>
      <Trigger Property="IsEnabled" Value="False">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
      </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>

  <ControlTemplate x:Key="MenuItemSubmenuItemTemplate" TargetType="{x:Type MenuItem}">
    <Border x:Name="Border" BorderThickness="1">
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto" SharedSizeGroup="Icon" />
          <ColumnDefinition Width="3" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="13" />
        </Grid.ColumnDefinitions>
        <ContentPresenter Name="Icon" ContentSource="Icon" Margin="{TemplateBinding Padding}" />
        <Border x:Name="Check" Grid.Column="0" Width="13" Height="13" Margin="6,0"
                Background="{DynamicResource {x:Static SystemColors.MenuBrushKey}}"
                BorderBrush="{DynamicResource {x:Static styles:ResourceKeys.BorderBrush}}"
                BorderThickness="1" Visibility="Collapsed">
          <Path x:Name="CheckMark" Stretch="Fill" Margin="1"
                          Data="M7,0 3,5 1,3 0,4 3,7 9,0 z"
                          Fill="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"
                          Visibility="Hidden" UseLayoutRounding="False" />
        </Border>
        <ContentPresenter Grid.Column="2" Margin="{TemplateBinding Padding}" ContentSource="Header" RecognizesAccessKey="True" />
      </Grid>
    </Border>
    <ControlTemplate.Triggers>
      <Trigger Property="ButtonBase.Command" Value="{x:Null}" />
      <Trigger Property="Icon" Value="{x:Null}">
        <Setter TargetName="Icon" Property="Visibility" Value="Hidden" />
      </Trigger>
      <Trigger Property="IsChecked" Value="True">
        <Setter TargetName="CheckMark" Property="Visibility" Value="Visible" />
      </Trigger>
      <Trigger Property="IsCheckable" Value="True">
        <Setter TargetName="Check" Property="Visibility" Value="Visible" />
        <Setter TargetName="Icon" Property="Visibility" Value="Hidden" />
      </Trigger>
      <Trigger Property="IsHighlighted" Value="True">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}" />
      </Trigger>
      <Trigger Property="IsEnabled" Value="False">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
        <Setter TargetName="Check" Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
        <Setter TargetName="CheckMark" Property="Stroke" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
      </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>

  <ControlTemplate x:Key="MenuItemSubmenuHeaderTemplate" TargetType="{x:Type MenuItem}">
    <Border x:Name="Border" BorderThickness="1">
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto" SharedSizeGroup="Icon" />
          <ColumnDefinition Width="3" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="13" />
        </Grid.ColumnDefinitions>
        <ContentPresenter x:Name="Icon" Margin="3,0" VerticalAlignment="Center" ContentSource="Icon" />
        <ContentPresenter Grid.Column="2" Margin="{TemplateBinding Padding}" ContentSource="Header" RecognizesAccessKey="True" />
        <Path x:Name="ArrowPath" Grid.Column="3" HorizontalAlignment="Center" VerticalAlignment="Center"
              Data="M 0 0 L 0 7 L 4 3.5 Z"
              Fill="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" />
        <Popup x:Name="Popup" Grid.Column="0" AllowsTransparency="True" Focusable="False" HorizontalOffset="1"
               VerticalOffset="-2" IsOpen="{TemplateBinding IsSubmenuOpen}" Placement="Right" PopupAnimation="Fade">
          <Border x:Name="SubmenuBorder"
                  Background="{DynamicResource {x:Static SystemColors.MenuBrushKey}}"
                  BorderBrush="{DynamicResource {x:Static styles:ResourceKeys.BorderBrush}}"
                  BorderThickness="1" SnapsToDevicePixels="True">
            <ScrollViewer CanContentScroll="True" Style="{StaticResource MenuScrollViewer}">
              <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="True" />
            </ScrollViewer>
          </Border>
        </Popup>
      </Grid>
    </Border>
    <ControlTemplate.Triggers>
      <Trigger Property="Icon" Value="{x:Null}">
        <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
      </Trigger>
      <Trigger Property="IsHighlighted" Value="True">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}" />
      </Trigger>
      <Trigger SourceName="Popup" Property="AllowsTransparency" Value="True">
        <Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="0" />
      </Trigger>
      <Trigger Property="IsEnabled" Value="False">
        <Setter Property="Foreground"
                Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
        <Setter TargetName="ArrowPath" Property="Fill"
                Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
      </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>

  <Style x:Key="{x:Static styles:ResourceKeys.MenuItemStyle}" TargetType="{x:Type MenuItem}">
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.MenuBrushKey}}" />
    <Style.Triggers>
      <Trigger Property="Role" Value="TopLevelHeader">
        <Setter Property="Control.Padding" Value="7,2,8,3"/>
        <Setter Property="Template" Value="{StaticResource MenuItemTopLevelHeaderTemplate}" />
      </Trigger>
      <Trigger Property="Role" Value="TopLevelItem">
        <Setter Property="Control.Padding" Value="7,2,8,3"/>
        <Setter Property="Template" Value="{StaticResource MenuItemTopLevelItemTemplate}" />
      </Trigger>
      <Trigger Property="Role" Value="SubmenuHeader">
        <Setter Property="Control.Padding" Value="2,3"/>
        <Setter Property="Template" Value="{StaticResource MenuItemSubmenuHeaderTemplate}" />
      </Trigger>
      <Trigger Property="Role" Value="SubmenuItem">
        <Setter Property="Control.Padding" Value="2,3"/>
        <Setter Property="Template" Value="{StaticResource MenuItemSubmenuItemTemplate}" />
      </Trigger>
    </Style.Triggers>
  </Style>

  <Style x:Key="{x:Static styles:ResourceKeys.ContextMenuStyle}" TargetType="{x:Type ContextMenu}">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}" />
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.MenuBrushKey}}" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Padding" Value="0" />
    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static styles:ResourceKeys.BorderBrush}}" />
    <Setter Property="HasDropShadow" Value="False" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type ContextMenu}">
          <Border x:Name="Border" Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}"
                  BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
            <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Cycle" />
          </Border>
          <ControlTemplate.Triggers>
            <Trigger Property="HasDropShadow" Value="False">
              <Setter TargetName="Border" Property="CornerRadius" Value="0" />
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <Style x:Key="{x:Static styles:ResourceKeys.MenuStyle}" TargetType="{x:Type Menu}">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.MenuBarBrushKey}}" />
    <Setter Property="Padding" Value="0" />
    <Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Contained" />
    <Setter Property="KeyboardNavigation.TabNavigation" Value="Once" />
  </Style>

  <ControlTemplate x:Key="MenuGroupItemTemplate" TargetType="{x:Type GroupItem}">
    <StackPanel>
      <Separator Style="{DynamicResource {x:Static styles:ResourceKeys.MenuItemSeparatorStyle}}"/>
      <ItemsPresenter />
    </StackPanel>
  </ControlTemplate>

  <ControlTemplate x:Key="FirstMenuGroupItemTemplate" TargetType="{x:Type GroupItem}">
    <ItemsPresenter />
  </ControlTemplate>

  <GroupStyle x:Key="MenuGroupStyle">
    <GroupStyle.ContainerStyle>
      <Style TargetType="GroupItem">
        <Setter Property="Template" Value="{StaticResource MenuGroupItemTemplate}"/>
        <Style.Triggers>
          <DataTrigger Binding="{Binding RelativeSource={RelativeSource PreviousData}}" Value="{x:Null}">
            <Setter Property="Template" Value="{StaticResource FirstMenuGroupItemTemplate}"/>
          </DataTrigger>
        </Style.Triggers>
      </Style>
    </GroupStyle.ContainerStyle>
  </GroupStyle>

  <Style x:Key="CompositeMenuItemStyle" TargetType="{x:Type MenuItem}" BasedOn="{StaticResource {x:Static styles:ResourceKeys.MenuItemStyle}}">
    <Setter Property="Header" Value="{Binding Header}"/>
    <Setter Property="Icon" Value="{Binding Icon, Converter={x:Static toms:ImageSourceToImageConverter.Default}}"/>
    <Setter Property="ToolTip" Value="{Binding Description}"/>
    <Setter Property="IsCheckable" Value="{Binding IsCheckable}"/>
    <Setter Property="toms:StyleBindings.GroupStyle" Value="{StaticResource MenuGroupStyle}"/>
    <Setter Property="toms:StyleBindings.GroupDescriptions">
      <Setter.Value>
        <toms:GroupDescriptionCollection>
          <PropertyGroupDescription PropertyName="GroupName"/>
        </toms:GroupDescriptionCollection>
      </Setter.Value>
    </Setter>
    <Setter Property="toms:StyleBindings.Behaviors">
      <Setter.Value>
        <toms:BehaviorCollection>
          <toms:ItemsControlCompositionBehavior RegionIdBinding="{Binding SubRegionId}"/>
        </toms:BehaviorCollection>
      </Setter.Value>
    </Setter>
    <Style.Triggers>
      <DataTrigger Binding="{Binding IsCheckable}" Value="True">
        <Setter Property="IsChecked" Value="{Binding IsChecked}"/>
      </DataTrigger>
      <Trigger Property="Role" Value="SubmenuItem">
        <Setter Property="Command" Value="{Binding Command}"/>
      </Trigger>
      <Trigger Property="Role" Value="TopLevelItem">
        <Setter Property="Command" Value="{Binding Command}"/>
      </Trigger>
    </Style.Triggers>
  </Style>

  <Style x:Key="{x:Static styles:ResourceKeys.CompositeContextMenuStyle}" TargetType="{x:Type ContextMenu}" BasedOn="{StaticResource {x:Static styles:ResourceKeys.ContextMenuStyle}}">
    <Setter Property="toms:StyleBindings.GroupStyle" Value="{StaticResource MenuGroupStyle}"/>
    <Setter Property="toms:StyleBindings.GroupDescriptions">
      <Setter.Value>
        <toms:GroupDescriptionCollection>
          <PropertyGroupDescription PropertyName="GroupName"/>
        </toms:GroupDescriptionCollection>
      </Setter.Value>
    </Setter>
    <Setter Property="ItemContainerStyle" Value="{StaticResource CompositeMenuItemStyle}"/>
  </Style>

</ResourceDictionary>